/*Author: Yuan Li
 *Course:15123
 *Last Modified Date:May 23 2011
 */

#include "stdio.h"
#include "stdlib.h"



typedef struct{
	int index;
	short value;
	char flag;
} sample;

void printResult(int endIndex, short endVal, int period,\
		short peak, short nadir)
{
	printf("%d\t%hi\t%d\t%hi\t%hi\n", endIndex, endVal,\
			period, peak, nadir);
}

int main(int argc,char** argv)
{
	FILE *input;
	short tmp,peak,nadir;
	int period,count;
	sample prev,curr,start;		

	if (argc < 2){
		printf("You must input the input file!!\n");
		exit(EXIT_FAILURE);
	}

	if (!(input = fopen(argv[1],"rb"))){
		printf("fail to open the %s \n",argv[1]);
		exit(EXIT_FAILURE);
	}

	/* initialize the variables */
	prev.index = 0;
	prev.value = 0;
	prev.flag = 0;
	curr.index = 0;
	curr.value = 0;
	curr.flag = 0;
	start.index = 0;
	start.value = 0;
	start.flag = 0;
	count = 0;    /* record the index of sample point*/
	peak = -2047;  /* the value of the sample will be within */
	nadir = 2048;  /* -2047 ~ 2048*/

	/* print header*/
	/*printf("Endindex\tEndValue\tPeriod\tPeak\tNadir\n");*/

	while((fread(&tmp,sizeof(tmp),1,input)) == 1){
		/* read a point	*/
		count++;
		curr.index = count;
		curr.value = tmp;
		curr.flag = 1;
		
		/* this part will be only used to 
		 * keep searching the first wave's start point.
		 * The start point will only be the point:
		 * 1: 0 and after a value below 0, or this point 
		 * is the first point.
		 * 2: the first positive value after zero.
		 */
		if (start.flag == 0){
			/* kinds of the ugly below*/
			/* find the start point for part of conditon 1*/
			if (curr.value == 0 && prev.flag == 0){
				start.index = curr.index;
				start.value = curr.value;
				start.flag = curr.flag;
			}
			/* find the start point for part condition 1 and full 2*/
			else if (prev.flag == 1){
				if (prev.value < 0 && curr.value >= 0){
					start.index = curr.index;
					start.value = curr.value;
					start.flag = curr.flag;
				}	
			
			}
			/* record current point as prev before reading 
			 * next point
			 */
		
			prev.index = curr.index;
			prev.value = curr.value;
			prev.flag = curr.flag;
			continue;
		
		}

		/* find the peak and nadir */
		peak = curr.value > peak? curr.value:peak;
		
		nadir = curr.value < nadir? curr.value:nadir;

		/* find the end point
		 * end point will appear in the second half period
		 * so the value will be below 0.
		 */
		if (prev.value < 0){
			/* 0 will be a ending point, and we also
			 * have to deal with the double count problem
			 */
			if (curr.value == 0){
				period = curr.index - start.index + 1;
				printResult(curr.index, curr.value, period,\
						peak, nadir);
				
				/* the next start point will be the curr point*/
				start.index = curr.index;
				start.value = curr.value;
				start.flag = curr.flag;

				/* restore peak and nadir */
				peak = -2047;
				nadir = 2048;
			
			}

			/* the first point above 0*/
			if (prev.value < 0 && curr.value > 0){
				period = prev.index - start.index + 1;
				printResult(prev.index, prev.value, period,\
						peak, nadir);

				/* the next start point will be curr point too*/
				start.index = curr.index;
				start.value = curr.value;
				start.flag = curr.flag;

				/* restore peak and nadir */
				peak = -2047;
				nadir = 2048;

			}
		}	

		/* save the prev point,before flush the curr point*/
		prev.index = curr.index;
		prev.value = curr.value;
		prev.flag = curr.flag;	
	}

	/* scan all the point in the file, safely quit*/	
	return 1;
}
